<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Refresh" content="600; url=gettaokedata.php" />
</head>

<body>
<?PHP
//echo phpinfo();
session_start();
date_default_timezone_set("Asia/Chongqing");
include("common/connect.php");
include("common/common.php");
include("taobaoapi/TopSdk.php");

echo "start<br>";

$dNow = date('Y-m-j G:i:s');
echo "今天是： " .$dNow;

// 上一次取的时间和上一次取的TradeId，这2个是用来作增量的依据
$lastDate = mktime(0, 0, 0, 9, 5, 2011);
$lastTradeId = "";
$lastItemId = "";
//echo date('Ymd', $lastDate);
//exit;

// 取上一个时间
if (1) {
$result = mysql_query( "select * from report_items order by pay_time desc limit 1" );
if ($result && mysql_num_rows($result) > 0)
{
	$row = mysql_fetch_assoc( $result );
	$lastDate = strtotime($row["pay_time"], time());
	$lastTradeId = $row["trade_id"];
	$lastItemId = $row["num_iid"];
	echo "<br>上一条记录时间是：".date('Y-m-j G:i:s', $lastDate) . " 对应TradeId是：" . $lastTradeId." Num_iid：".$lastItemId;
	$lastDate = strtotime(date('Y-m-j', strtotime($row["pay_time"], time())));
}
}

//$lastDate = mktime(0, 0, 0, 12, 6, 2011);					// 直接指定
$nNeedDay = intval((time() - $lastDate) / (3600 * 24));
$nNeedDay = min($nNeedDay, 10);	

//echo "<br>".strtotime(time());
//echo "<br>".time()."  ".$lastDate ."  ".(time() - $lastDate);
echo "<br>需要取历史 ".$nNeedDay." 天的记录.";


//GetOneDateData("20110314", "");
//exit;

// 开始取
$nAllCount = 0;
for ($i = 0; $i < $nNeedDay; ++$i)
{
	echo "<br>";
	$dCurDate = $lastDate + $i * 3600 * 24;
	echo date('Y-m-j', $dCurDate);
	GetOneDateData("", "", "", 2, date('Ymd', $dCurDate), $lastTradeId, $lastItemId, $nAllCount);
//	GetOneDateData("12356312", "ab9de83ee8af5879abfddf7aeea489ef", 2, date('Ymd', $dCurDate), $lastTradeId, $lastItemId, $nAllCount);
//	GetOneDateData("12357230", "0a4771e3a539e1717ce766cfbfc2a990", 2, date('Ymd', $dCurDate), $lastTradeId, $lastItemId, $nAllCount);
//	GetOneDateData("12357234", "33668a5c49f1fafcc83a934767090c54", 2, date('Ymd', $dCurDate), $lastTradeId, $lastItemId, $nAllCount);
//	GetOneDateData("12357236", "678c99e4f8afe1d0e7ed4cbf79595c3f", 2, date('Ymd', $dCurDate), $lastTradeId, $lastItemId, $nAllCount);
//	GetOneDateData("12357238", "47758d39a201f71f12a445c17a92b6db", 2, date('Ymd', $dCurDate), $lastTradeId, $lastItemId, $nAllCount);

//	GetOneDateData("12175045", "fecb88ed680dc756af6fef24cc62b27f", 0, date('Ymd', $dCurDate), $lastTradeId, $lastItemId, $nAllCount);	// 有可能是被禁掉的老账号，应该没了
//	GetOneDateData("12322390", "33184e84dfc3760bf4b8d7c80474bf6c", 1, date('Ymd', $dCurDate), $lastTradeId, $lastItemId, $nAllCount);	// 强B的，已经没用了
}

if(1) {
echo "<br>开始取今天的记录: ".date('Y-m-j G:i:s');
GetOneDateData("", "", "", 2, date('Ymd'), $lastTradeId, $lastItemId, $nAllCount);
//GetOneDateData("12356312", "ab9de83ee8af5879abfddf7aeea489ef", 2, date('Ymd'), $lastTradeId, $lastItemId, $nAllCount);
//GetOneDateData("12357230", "0a4771e3a539e1717ce766cfbfc2a990", 2, date('Ymd'), $lastTradeId, $lastItemId, $nAllCount);
//GetOneDateData("12357234", "33668a5c49f1fafcc83a934767090c54", 2, date('Ymd'), $lastTradeId, $lastItemId, $nAllCount);
//GetOneDateData("12357236", "678c99e4f8afe1d0e7ed4cbf79595c3f", 2, date('Ymd'), $lastTradeId, $lastItemId, $nAllCount);
//GetOneDateData("12357238", "47758d39a201f71f12a445c17a92b6db", 2, date('Ymd'), $lastTradeId, $lastItemId, $nAllCount);

//GetOneDateData("12175045", "fecb88ed680dc756af6fef24cc62b27f", 0, date('Ymd'), $lastTradeId, $lastItemId, $nAllCount);	// 有可能是被禁掉的老账号，应该没了
//GetOneDateData("12322390", "33184e84dfc3760bf4b8d7c80474bf6c", 1, date('Ymd'), $lastTradeId, $lastItemId, $nAllCount);	// 强B的，已经没用了
}

function GetRndKey()
{
	$key = array(
	array("12356312", "ab9de83ee8af5879abfddf7aeea489ef", "4021332bf700ca3c1ff8139f0f5e388c79bbba1Tcy9ZB7n7563050351"),
	array("12357230", "0a4771e3a539e1717ce766cfbfc2a990", "4021300ZyDZvlod07d3ed9cd8029b08c13a59b4f1ac37237563050351"),
	array("12357234", "33668a5c49f1fafcc83a934767090c54", "40213337d90bdf1b088870d22051978eca29d7c7biyc93fJ563050351"),
	array("12357236", "678c99e4f8afe1d0e7ed4cbf79595c3f", "40213016FzbEIqCT8da07dbeeab0330da2fdd28d557d7367563050351"),
	array("12357238", "47758d39a201f71f12a445c17a92b6db", "6100911d232a2d736fa1acc9a58e32a953388ceb4281142756305035"),
	);
//	echo $key[0];
//	echo rand(0, 4);
	return $key[rand(0, 4)];
}
function GetOneDateData($appkey, $secretKey, $sessionKey, $a_no, $strCurDay, $lastTradeId, $lastItemId, &$nAllCount)
{
	if ($appkey == "")
	{
		$key = GetRndKey();
		$appkey = $key[0];		// $appkey;
		$secretKey = $key[1];	//$secretKey;
		$sessionKey = $key[2];	//$sessionKey;
	}
	$c = new TopClient;
	$c->appkey = $appkey;
	$c->secretKey = $secretKey;
	$req = new TaobaokeReportGetRequest;
	$req->setFields("trade_id,pay_time,pay_price,num_iid,outer_code,commission_rate,commission,seller_nick,app_key,item_num,item_title,category_id,category_name,shop_title,real_pay_fee");
	$req->setPageSize(40);

	echo "<br>GetOneDateData, day=".$strCurDay." key:".$appkey;
	$req->setDate($strCurDay);
	
	$strFirstTradeId = "";	$strFirstPayTime = ""; $strFirstNumIID = "";
	for ($page = 1; $page < 100; ++$page)
	{
		echo "<br>page:" .$page. "<br>";
		$req->setPageNo($page);
		$resp="";
		$resp = $c->execute($req, $sessionKey);
		if ($resp->getName() == "error_response")
		{
			echo "error_response";
			exit;
		}
	
		$breakFlag = 0;
		$nLastCount = $nAllCount;
		$bFirstItem = 1;
		foreach ($resp->taobaoke_report->taobaoke_report_members[0] as $Item)
		{
			
			if ($bFirstItem == 1)
			{
				if (strcmp($strFirstTradeId,$Item->trade_id) == 0 &&
					strcmp($strFirstPayTime, $Item->pay_time) == 0 &&
					strcmp($strFirstNumIID, $Item->num_iid) == 0)
				{
					$breakFlag = 1;
					echo "<br>重复了，应该是取完了~<br>";
					break;
				}
				
				$strFirstTradeId = $Item->trade_id;
				$strFirstPayTime = $Item->pay_time;
				$strFirstNumIID = $Item->num_iid;
				$bFirstItem = 0;
			}
			if(0){
			if (strcmp($Item->trade_id, $lastTradeId) == 0 && strcmp($Item->num_iid, $lastItemId) == 0)
			{
				echo "<br>取到上一条TradeId，增量已经取完， TradeId： ".$lastTradeId."时间是：".$Item->pay_time;
				$breakFlag = 1;
				break;
			}
			}
			
			//
			echo $nAllCount;
			echo "   |   ";
			echo $Item->pay_time;
			echo "   |   ";
			echo $Item->seller_nick;
			echo "   |   ";
			echo $Item->outer_code;
			echo "<br>";
			$nAllCount += 1;
			
			$sql = "insert into report_items (			app_key,trade_id,pay_time,pay_price,num_iid,outer_code,commission_rate,commission,seller_nick,item_num,item_title,category_id,category_name,shop_title,real_pay_fee,a_no) 
				values (
					'".$Item->app_key."',
					'".$Item->trade_id."',
					'".$Item->pay_time."',
					'".$Item->pay_price."',
					'".$Item->num_iid."',
					'".$Item->outer_code."',
					'".$Item->commission_rate."',
					'".$Item->commission."',
					'".mysql_real_escape_string($Item->seller_nick)."',
					'".$Item->item_num."',
					'".mysql_real_escape_string($Item->item_title)."',
					'".$Item->category_id."',
					'".mysql_real_escape_string($Item->category_name)."',
					'".mysql_real_escape_string($Item->shop_title)."',
					'".$Item->real_pay_fee."',
					$a_no
					)";
			mysql_query( $sql );
			if (mysql_error())
			{
				echo "已存在:".mysql_error()."--";
			}
			if (mysql_affected_rows() == 0)
			{
				//echo "<script>alert('处理过程中发生失败，请告诉程序员，出错代码：1');/script>";
                echo "----未改变";
			}
		}
		
		
		if ($breakFlag == 1)
			break;
			
		if (0) {
		if ($nLastCount == $nAllCount || $nAllCount % 40 != 0)	// 淘宝api有点怪，不知道判断已经取完了
			break;
		}
	}
}

echo "<br>";
echo $nAllCount;
echo "<br>end";
//print_r($resp);
?>


</body>
</html>